<?php

namespace App\Repositories\Eloquent\Mgt;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

class SysRepository extends Repository
{

    /**
     * @param App
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Mgt\SysConfig';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($query = '')
    {
        // 获取分页数据
        if (!$query) {
            $result = $this->model
                ->orderBy('order', 'asc');
        } else {
            // 若有查询参数
            $result = $this->model
                ->where('name', 'like', "%$query%");
        }

        return $result;
    }

   
    /**
     * 获取微信培训栏目配置(type==1)
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function columns()
    {
        return $result = $this->model
                ->where('type', 1)
                ->get()->map(function ($result) {
                    $result->props = collection(unserialize($result->value));
                    return $result;
                });
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($name)
    {
        return $result = $this->model
                ->where('name', $name)
                ->first();
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function renew($request, $id)
    {
        $debug = $this->update([
            'value' => $request->debug==1?1:0,            
        ], $id);
        
        if( !!$debug ){           
            return $this->responseSuccess('设置成功');
        }
        
        return $this->responseFail(500, '设置失败');
    }

    
    public function columnUpdate($request)
    {
        $names = ['cfg_column1', 'cfg_column2', 'cfg_column3','cfg_column4'];
        
        $count = count($request->name);

        for($i = 0; $i <= $count -1 ; $i++){
            $arr[$i][] = $request->name[$i];
            $arr[$i][] = $request->link[$i];
            $arr[$i][] = $request->icon[$i];
            $arr[$i][] = $request->desc[$i];
        }

        foreach($names as $key=>$name){

            $cfg = $this->show($name);
            
            $result = \App\Models\Mgt\SysConfig::where('id', $cfg->id)->update( ['value'=>serialize($arr[$key])] );
        }

        return $this->responseSuccess('设置成功', $arr);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    
}